package fr.noe.amicaleloisirs.concours.view;

import java.util.List;

import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;

import fr.noe.amicaleloisirs.concours.business.Team;
import fr.noe.amicaleloisirs.concours.utils.HTMLUtil;
import fr.noe.amicaleloisirs.concours.utils.IndexGetter;

public class ClassementView extends CellTable<Team> {

    public ClassementView(List<Team> teams) {

        // create a table with the list size
        super(teams.size());

        IndexGetter.get().reset();


        /********************************************************/
        /*               initialize column renderers            */
        /********************************************************/

        TextColumn<Team> idColumn = new TextColumn<Team>() {
            @Override
            public String getValue(Team object) {
                return IndexGetter.get().nextInt() + "";
            }
        };

        TextColumn<Team> lastnameColumn = new TextColumn<Team>() {
            @Override
            public String getValue(Team object) {
                return object.getPlayer1().getFirstname() + " "
                        + object.getPlayer1().getLastname() + " / "
                        + object.getPlayer2().getFirstname() + " "
                        + object.getPlayer2().getLastname();
            }
        };

        TextColumn<Team> nbVictoireColumn = new TextColumn<Team>() {
            @Override
            public String getValue(Team object) {
                return object.getNombreVictoires() + "";
            }
        };

        TextColumn<Team> nbDefaiteColumn = new TextColumn<Team>() {
            @Override
            public String getValue(Team object) {
                return object.getNombreDefaites() + "";
            }
        };

        TextColumn<Team> serieColumn = new TextColumn<Team>() {
            @Override
            public void render(Context context, Team object, SafeHtmlBuilder sb) {
                sb.appendHtmlConstant(getValue(object));
            }

            @Override
            public String getValue(Team object) {
                return HTMLUtil.formatSerie(object.getSerie());
            }
        };

        TextColumn<Team> pointsPourColumn = new TextColumn<Team>() {
            @Override
            public String getValue(Team object) {
                return object.getTotalPour() + "";
            }
        };

        TextColumn<Team> pointsContreColumn = new TextColumn<Team>() {
            @Override
            public String getValue(Team object) {
                return object.getTotalContre() + "";
            }
        };


        /********************************************************/
        /*              initialize column headers               */
        /********************************************************/

        addColumn(idColumn, " ");
        addColumn(lastnameColumn, "Equipe");
        addColumn(nbVictoireColumn, "Parties gagnées");
        addColumn(nbDefaiteColumn, "Parties perdues");
        addColumn(serieColumn, "Série");
        addColumn(pointsPourColumn, "Points marqués");
        addColumn(pointsContreColumn, "Points encaissés");

        // Push the data into the widget.
        setRowData(0, teams);
        setRowCount(teams.size(), true);
        
        setStyleName("table withShadow");

        setVisible(true);

    }


}

